#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e6 + 5;

int n, m;
int tr[1005][26], fail[1005], tot;
vector<int> len[N];
int dp[N];
int newnode() { return ++tot; }
void insert(string s) {
  int p = 0;
  for (char c : s) {
    int ch = c - 'a';
    if (!tr[p][ch]) tr[p][ch] = newnode();
    p = tr[p][ch];
  }
  len[p].push_back(s.size());
}

void build_ac() {
  queue<int> q;
  rep(ch, 0, 25) if (tr[0][ch]) q.push(tr[0][ch]);
  while (q.size()) {
    int p = q.front();
    q.pop();
    rep(ch, 0, 25) {
      if (tr[p][ch]) {
        q.push(tr[p][ch]);
        fail[tr[p][ch]] = tr[fail[p]][ch];
        int fp = fail[tr[p][ch]];
        for (int x : len[fp]) len[tr[p][ch]].push_back(x);
      } else {
        tr[p][ch] = tr[fail[p]][ch];
      }
    }
  }
}
int search(string s) {
  int res = 0;
  int n = s.size();
  int p = 0;
  dp[0] = 1;
  rep(i, 1, n) {
    if (res + 20 < i) return res;
    dp[i] = 0;
    int ch = s[i - 1] - 'a';
    p = tr[p][ch];
    for (int x : len[p]) {
      if (dp[i - x]) {
        dp[i] = 1;
        res = max(res, i);
        break;
      }
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) {
    string s;
    cin >> s;
    insert(s);
  }
  build_ac();
  while (m--) {
    string s;
    cin >> s;
    cout << search(s) << endl;
  }
  return 0;
}